package com.meritdata.cloud.data.standard.directory.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.meritdata.cloud.data.base.ResultData;
import com.meritdata.cloud.data.standard.directory.dao.IDataDomainDao;
import com.meritdata.cloud.data.standard.directory.entity.TdDataDomain;
import com.meritdata.cloud.data.standard.directory.entity.TdDataLayer;
import com.meritdata.cloud.data.standard.directory.service.IDataDomainService;

@Service
public class DataDomainService implements IDataDomainService {

	@Autowired
	private IDataDomainDao dataDomainDao;

	@Override
	public ResultData create(String name, String code, String description, Long layerId, Long domainId, String userId) {
		ResultData resultData = new ResultData();

		if (isExistCode(layerId, domainId, code, 0L)) {
			resultData.setStatus(false);
			resultData.setMsg("编码已存在");
			return resultData;
		}

		if (isExistName(layerId, domainId, name, 0L)) {
			resultData.setStatus(false);
			resultData.setMsg("名称已存在");
			return resultData;
		}

		TdDataDomain dataDomain = new TdDataDomain();
		dataDomain.setCode(code);
		dataDomain.setCreateTime(new Date());
		dataDomain.setCreator(userId);
		dataDomain.setDescription(description);
		dataDomain.setName(name);
		dataDomain.setDomainId(domainId);
		dataDomain.setLayerId(layerId);
		dataDomainDao.save(dataDomain);
		resultData.setData(dataDomain);
		return resultData;
	}

	@Override
	public ResultData delete(Long id) {
		ResultData resultData = new ResultData();
		List<TdDataDomain> domains = dataDomainDao.findEqualField("domainId", id);
		if (!CollectionUtils.isEmpty(domains)) {
			resultData.setStatus(false);
			resultData.setMsg("数据域不为空，不允许删除");
			return resultData;
		}
		int delete = dataDomainDao.delete(id);
		resultData.setStatus(delete > 0);
		return resultData;
	}

	@Override
	public ResultData update(String name, String code, String description, Long id, String userId) {
		ResultData resultData = new ResultData();

		TdDataDomain dataDomain = dataDomainDao.findOne(id);
		if (dataDomain == null) {
			resultData.setStatus(false);
			resultData.setMsg("数据域已不存在");
			return resultData;
		}

		if (isExistCode(dataDomain.getLayerId(), dataDomain.getDomainId(), code, id)) {
			resultData.setStatus(false);
			resultData.setMsg("编码已存在");
			return resultData;
		}

		if (isExistName(dataDomain.getLayerId(), dataDomain.getDomainId(), name, id)) {
			resultData.setStatus(false);
			resultData.setMsg("名称已存在");
			return resultData;
		}

		dataDomain.setReviserId(userId);
		dataDomain.setUpdateTime(new Date());
		dataDomain.setCode(code);
		dataDomain.setName(name);
		dataDomain.setDescription(description);

		dataDomainDao.update(dataDomain);
		resultData.setData(dataDomain);
		return resultData;
	}

	@Override
	public TdDataDomain get(Long id) {
		return dataDomainDao.findOne(id);
	}

	private boolean isExistName(Long layerId, Long domainId, String name, Long id) {
		long count;
		if (domainId != null) {
			count = dataDomainDao.countHql("from TdDataDomain t where t.domainId = ? and t.name = ? and t.id <> ?",
					domainId, name, id);
		} else {
			count = dataDomainDao.countHql("from TdDataDomain t where t.layerId = ? and t.name = ? and t.id <> ?",
					layerId, name, id);
		}
		return count > 0;
	}

	private boolean isExistCode(Long layerId, Long domainId, String code, Long id) {
		long count;
		if (domainId != null) {
			count = dataDomainDao.countHql("from TdDataDomain t where t.domainId = ? and t.code = ? and t.id <> ?",
					domainId, code, id);
		} else {
			count = dataDomainDao.countHql("from TdDataDomain t where t.layerId = ? and t.code = ? and t.id <> ?",
					layerId, code, id);
		}
		return count > 0;
	}

	@Override
	public List<TdDataDomain> listByLayerId(Long layerId) {
		return dataDomainDao.findEqualField("layerId", layerId);
	}

	@Override
	public List<TdDataDomain> listByDomainId(Long domainId) {
		return dataDomainDao.findEqualField("domainId", domainId);
	}

}
